<template>
  <div class="app-container">
    <!-- 条件查询 -->
    <el-form :inline="true" :model="query" size="mini">
      <el-form-item label="分类名称:">
        <el-input v-model.trim="query.name"></el-input>
      </el-form-item>
      <el-form-item label="状态:">
        <el-select
          v-model="query.status"
          clearable
          filterable
          style="width: 85px"
        >
          <el-option
            v-for="i in statusOptions"
            :key="i.code"
            :label="i.name"
            :value="i.code"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" type="primary" @click="queryData"
          >查询</el-button
        >
        <el-button icon="el-icon-refresh" class="filter-item" @click="reload"
          >重置</el-button
        >
        <el-button
          type="primary"
          @click="formAdd"
          size="mini"
          icon="el-icon-circle-plus-outline"
          >新增</el-button
        >
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table
      :data="list"
      stripe
      style="width: 100%"
      border
      highlight-current-row
    >
      <el-table-column align="center" type="index" label="序号" width="60">
      </el-table-column>
      <el-table-column align="center" prop="name" label="分类名称">
      </el-table-column>
      <el-table-column align="center" prop="sort" label="排序">
      </el-table-column>
      <el-table-column align="center" prop="remark" label="备注">
      </el-table-column>
      <el-table-column align="center" prop="status" label="状态">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status | statusFilts">{{
            scope.row.status ? "正常" : "禁用"
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            @click="handleEdit(scope.row.id)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pageination
      :currentPage="page.current"
      :pageSizeInfo="page.size"
      :total="page.total"
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange"
    />
    <!-- 弹窗 -->
    <edit
      :dialogVisible="edit.dialogVisible"
      :title="edit.title"
      :formData="edit.formData"
      :remoteForm="remoteForm"
    />
  </div>
</template>

<script>
import { ApiGetList, ApiById, ApiDelId } from '@/api/category'
import pageination from '@/components/common/pageination.vue'
import edit from './components/edit.vue'
// 用于下拉框
const statusOptions = [
  { code: 0, name: '禁用' },
  { code: 1, name: '正常' }
]

export default {
  name: 'Category',
  components: {
    pageination,
    edit
  },
  data () {
    return {
      list: [],
      page: {
        current: 1,
        size: 20,
        total: 0
      },
      query: {},
      statusOptions,
      edit: {
        dialogVisible: false,
        title: '新增',
        formData: {}
      }
    }
  },
  filters: {
    statusFilts (status) {
      // 0 禁用 1 正常
      const statusMap = { 0: 'danger', 1: 'success' }
      return statusMap[status]
    }
  },
  methods: {
    async fetchData () {
      const { data: res } = await ApiGetList(this.query, this.page.size, this.page.current)
      this.list = res.records
      this.page.total = res.total
    },
    async handleEdit (id) { // 编辑操作
      const res = await ApiById(id)
      if (res.code === 20000) {
        this.edit.title = '编辑'
        this.edit.dialogVisible = true
        this.edit.formData = res.data
      }
    },
    handleDelete (id) { // 删除操作
      this.$confirm('确认删除这条记录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await ApiDelId(id)
        this.$message({
          type: res.code === 20000 ? 'success' : 'warn',
          message: res.message
        });
        // 刷新列表
        this.fetchData()
      }).catch(() => {

      });
    },
    // 当前条数
    handleSizeChange (val) {
      this.page.size = val
      this.fetchData()
    },
    // 当前页数
    handleCurrentChange (val) {
      this.page.current = val
      this.fetchData()
    },
    queryData () {
      this.page.current = 1
      this.fetchData()
    },
    reload () {
      this.query = {}
      this.fetchData()
    },
    remoteForm () { // 关闭弹窗
      this.edit.formData = {}
      this.edit.dialogVisible = false
    },
    formAdd () { // 新增操作
      this.edit.title = '新增'
      this.edit.dialogVisible = true
    }
  },
  created () {
    this.fetchData()
  },
}
</script>

<style lang="scss" scoped>
.app-container {
  padding: 20px;
  .el-table {
    margin-bottom: 20px;
  }
}
</style>